﻿#include "base/xrtc_preview.h"

#include <rtc_base/task_utils/to_queued_task.h>
#include <rtc_base/logging.h>

namespace xrtc {

XRTCPreview::XRTCPreview(
    rtc::VideoSourceInterface<webrtc::VideoFrame>* video_source, 
    const XRTCCanvas& canvas) :
    current_thread_(rtc::Thread::Current()),
    d3d9_render_(std::make_unique<D3D9Render>(canvas.view,
        video_source))
{
}

XRTCPreview::~XRTCPreview() {
}

void XRTCPreview::Start() {
    current_thread_->PostTask(webrtc::ToQueuedTask([=]() {
        if (running_) {
            RTC_LOG(LS_WARNING) << "xrtc preview already start, ignore";
            return;
        }

        running_ = true;

        d3d9_render_->Start();
    }));
}

void XRTCPreview::Stop() {
    current_thread_->PostTask(webrtc::ToQueuedTask([=]() {
        if (!running_) {
            RTC_LOG(LS_WARNING) << "xrtc preview already stop, ignore";
            return;
        }

        running_ = false;

        d3d9_render_->Stop();
    }));
}

void XRTCPreview::Resize() {
    current_thread_->PostTask(webrtc::ToQueuedTask([=]() {
        if (d3d9_render_) {
            d3d9_render_->Resize();
        }
    }));
}


} // namespace xrtc